{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "631b6e35",
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "#定义算法\n",
    "def kennardstonealgorithm(x_variables, k):\n",
    "    x_variables = np.array(x_variables)\n",
    "    original_x = x_variables\n",
    "    distance_to_average = ((x_variables - np.tile(x_variables.mean(axis=0), (x_variables.shape[0], 1))) ** 2).sum(\n",
    "        axis=1)\n",
    "    max_distance_sample_number = np.where(distance_to_average == np.max(distance_to_average))\n",
    "    max_distance_sample_number = max_distance_sample_number[0][0]\n",
    "    selected_sample_numbers = list()\n",
    "    selected_sample_numbers.append(max_distance_sample_number)\n",
    "    remaining_sample_numbers = np.arange(0, x_variables.shape[0], 1)\n",
    "    x_variables = np.delete(x_variables, selected_sample_numbers, 0)\n",
    "    remaining_sample_numbers = np.delete(remaining_sample_numbers, selected_sample_numbers, 0)\n",
    "    for iteration in range(1, k):\n",
    "        selected_samples = original_x[selected_sample_numbers, :]\n",
    "        min_distance_to_selected_samples = list()\n",
    "        for min_distance_calculation_number in range(0, x_variables.shape[0]):\n",
    "            distance_to_selected_samples = ((selected_samples - np.tile(x_variables[min_distance_calculation_number, :],\n",
    "                                                                        (selected_samples.shape[0], 1))) ** 2).sum(\n",
    "                axis=1)\n",
    "            min_distance_to_selected_samples.append(np.min(distance_to_selected_samples))\n",
    "        max_distance_sample_number = np.where(\n",
    "            min_distance_to_selected_samples == np.max(min_distance_to_selected_samples))\n",
    "        max_distance_sample_number = max_distance_sample_number[0][0]\n",
    "        selected_sample_numbers.append(remaining_sample_numbers[max_distance_sample_number])\n",
    "        x_variables = np.delete(x_variables, max_distance_sample_number, 0)\n",
    "        remaining_sample_numbers = np.delete(remaining_sample_numbers, max_distance_sample_number, 0)\n",
    "\n",
    "    return selected_sample_numbers, remaining_sample_numbers\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "203dff23",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "初级融合2:1\n",
      "训练集样本编号:\n",
      "[39 19 14 26  7 38 30 20 42 12  2 17 41 22  3 27 36 32 13 10  1 40 24 16\n",
      " 34 18  5  8]\n",
      "测试集样本编号:\n",
      "[ 4  6  9 11 15 21 23 25 28 29 31 33 35 37 43]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "\n",
    "# 读取Excel文件，并假设第一列是样本编号，其余列是特征数据\n",
    "df = pd.read_excel(\"F:\\\\研究\\\\大黄不同月份液体近红外KS划分\\\\10.25\\\\初级融合.xlsx\", sheet_name='Sheet1')\n",
    "# 第一列是样本编号，其余是特征数据\n",
    "sample_ids = df.iloc[:, 0]  # 保存样本编号\n",
    "x_variables = df.iloc[:, 1:].values  # 转换特征数据为numpy数组\n",
    "\n",
    "# 确保x_variables只包含数值数据\n",
    "x_variables = np.array(x_variables, dtype=float)\n",
    "\n",
    "# 调用k-s算法函数\n",
    "a = kennardstonealgorithm(x_variables, 28)\n",
    "# 得到训练集和测试集索引值\n",
    "train = a[0]\n",
    "test = a[1]\n",
    "# 使用索引值从原始DataFrame中获取样本编号\n",
    "train_samples_ids = sample_ids[train].values\n",
    "test_samples_ids = sample_ids[test].values\n",
    "\n",
    "# 输出结果\n",
    "print(\"初级融合2:1\\n训练集样本编号:\")\n",
    "print(train_samples_ids)\n",
    "print(\"测试集样本编号:\")\n",
    "print(test_samples_ids)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "249f5759",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "初级融合3:1\n",
      "训练集样本编号:\n",
      "[39 19 14 26  7 38 30 20 42 12  2 17 41 22  3 27 36 32 13 10  1 40 24 16\n",
      " 34 18  5  8  9 37 28 35]\n",
      "测试集样本编号:\n",
      "[ 4  6 11 15 21 23 25 29 31 33 43]\n"
     ]
    }
   ],
   "source": [
    "# 调用k-s算法函数\n",
    "a = kennardstonealgorithm(x_variables, 32)\n",
    "# 得到训练集和测试集索引值\n",
    "train = a[0]\n",
    "test = a[1]\n",
    "# 使用索引值从原始DataFrame中获取样本编号\n",
    "train_samples_ids = sample_ids[train].values\n",
    "test_samples_ids = sample_ids[test].values\n",
    "\n",
    "# 输出结果\n",
    "print(\"初级融合3:1\\n训练集样本编号:\")\n",
    "print(train_samples_ids)\n",
    "print(\"测试集样本编号:\")\n",
    "print(test_samples_ids)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "12a7e7fc",
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "初级融合4:1\n",
      "训练集样本编号:\n",
      "[39 19 14 26  7 38 30 20 42 12  2 17 41 22  3 27 36 32 13 10  1 40 24 16\n",
      " 34 18  5  8  9 37 28 35 33 11]\n",
      "测试集样本编号:\n",
      "[ 4  6 15 21 23 25 29 31 43]\n"
     ]
    }
   ],
   "source": [
    "#调用k-s算法函数\n",
    "a=kennardstonealgorithm(x_variables, 34)\n",
    "#得到训练集和测试集索引值\n",
    "train=a[0]\n",
    "test=a[1]\n",
    "# 使用索引值从原始DataFrame中获取样本编号\n",
    "train_samples_ids = sample_ids[train].values\n",
    "test_samples_ids = sample_ids[test].values\n",
    "\n",
    "# 输出结果\n",
    "print(\"初级融合4:1\\n训练集样本编号:\")\n",
    "print(train_samples_ids)\n",
    "print(\"测试集样本编号:\")\n",
    "print(test_samples_ids)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "520a1505",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "初级融合5:1\n",
      "训练集样本编号:\n",
      "[39 19 14 26  7 38 30 20 42 12  2 17 41 22  3 27 36 32 13 10  1 40 24 16\n",
      " 34 18  5  8  9 37 28 35 33 11 43  6]\n",
      "测试集样本编号:\n",
      "[ 4 15 21 23 25 29 31]\n"
     ]
    }
   ],
   "source": [
    "#调用k-s算法函数\n",
    "a=kennardstonealgorithm(x_variables, 36)\n",
    "#得到训练集和测试集索引值\n",
    "train=a[0]\n",
    "test=a[1]\n",
    "# 使用索引值从原始DataFrame中获取样本编号\n",
    "train_samples_ids = sample_ids[train].values\n",
    "test_samples_ids = sample_ids[test].values\n",
    "\n",
    "# 输出结果\n",
    "print(\"初级融合5:1\\n训练集样本编号:\")\n",
    "print(train_samples_ids)\n",
    "print(\"测试集样本编号:\")\n",
    "print(test_samples_ids)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "72a3f6b4",
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "NIR2:1\n",
      "训练集样本编号:\n",
      "[39 19 14 26  7 38 30 20 42 12  2 41 22  3 15 27 36 32 10  1 31 24 34 16\n",
      " 37 13 18  5]\n",
      "测试集样本编号:\n",
      "[ 4  6  8  9 11 17 21 23 25 28 29 33 35 40 43]\n"
     ]
    }
   ],
   "source": [
    "# 读取Excel文件，并假设第一列是样本编号，其余列是特征数据\n",
    "df = pd.read_excel(\"F:\\\\研究\\\\大黄不同月份液体近红外KS划分\\\\10.25\\\\NIR.xlsx\", sheet_name='Sheet1')\n",
    "# 第一列是样本编号，其余是特征数据\n",
    "sample_ids = df.iloc[:, 0]  # 保存样本编号\n",
    "x_variables = df.iloc[:, 1:].values  # 转换特征数据为numpy数组\n",
    "\n",
    "# 确保x_variables只包含数值数据\n",
    "x_variables = np.array(x_variables, dtype=float)\n",
    "\n",
    "# 调用k-s算法函数\n",
    "a = kennardstonealgorithm(x_variables, 28)\n",
    "# 得到训练集和测试集索引值\n",
    "train = a[0]\n",
    "test = a[1]\n",
    "# 使用索引值从原始DataFrame中获取样本编号\n",
    "train_samples_ids = sample_ids[train].values\n",
    "test_samples_ids = sample_ids[test].values\n",
    "\n",
    "# 输出结果\n",
    "print(\"NIR2:1\\n训练集样本编号:\")\n",
    "print(train_samples_ids)\n",
    "print(\"测试集样本编号:\")\n",
    "print(test_samples_ids)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "e1c12a4f",
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "NIR3:1\n",
      "训练集样本编号:\n",
      "[39 19 14 26  7 38 30 20 42 12  2 41 22  3 15 27 36 32 10  1 31 24 34 16\n",
      " 37 13 18  5 35 33  8 43]\n",
      "测试集样本编号:\n",
      "[ 4  6  9 11 17 21 23 25 28 29 40]\n"
     ]
    }
   ],
   "source": [
    "# 调用k-s算法函数\n",
    "a = kennardstonealgorithm(x_variables, 32)\n",
    "# 得到训练集和测试集索引值\n",
    "train = a[0]\n",
    "test = a[1]\n",
    "# 使用索引值从原始DataFrame中获取样本编号\n",
    "train_samples_ids = sample_ids[train].values\n",
    "test_samples_ids = sample_ids[test].values\n",
    "\n",
    "# 输出结果\n",
    "print(\"NIR3:1\\n训练集样本编号:\")\n",
    "print(train_samples_ids)\n",
    "print(\"测试集样本编号:\")\n",
    "print(test_samples_ids)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "688e8edb",
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "NIR4:1\n",
      "训练集样本编号:\n",
      "[39 19 14 26  7 38 30 20 42 12  2 41 22  3 15 27 36 32 10  1 31 24 34 16\n",
      " 37 13 18  5 35 33  8 43 25  9]\n",
      "测试集样本编号:\n",
      "[ 4  6 11 17 21 23 28 29 40]\n"
     ]
    }
   ],
   "source": [
    "#调用k-s算法函数\n",
    "a=kennardstonealgorithm(x_variables, 34)\n",
    "#得到训练集和测试集索引值\n",
    "train=a[0]\n",
    "test=a[1]\n",
    "# 使用索引值从原始DataFrame中获取样本编号\n",
    "train_samples_ids = sample_ids[train].values\n",
    "test_samples_ids = sample_ids[test].values\n",
    "\n",
    "# 输出结果\n",
    "print(\"NIR4:1\\n训练集样本编号:\")\n",
    "print(train_samples_ids)\n",
    "print(\"测试集样本编号:\")\n",
    "print(test_samples_ids)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "36457d7f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "NIR5:1\n",
      "训练集样本编号:\n",
      "[39 19 14 26  7 38 30 20 42 12  2 41 22  3 15 27 36 32 10  1 31 24 34 16\n",
      " 37 13 18  5 35 33  8 43 25  9 40 29]\n",
      "测试集样本编号:\n",
      "[ 4  6 11 17 21 23 28]\n"
     ]
    }
   ],
   "source": [
    "#调用k-s算法函数\n",
    "a=kennardstonealgorithm(x_variables, 36)\n",
    "#得到训练集和测试集索引值\n",
    "train=a[0]\n",
    "test=a[1]\n",
    "# 使用索引值从原始DataFrame中获取样本编号\n",
    "train_samples_ids = sample_ids[train].values\n",
    "test_samples_ids = sample_ids[test].values\n",
    "\n",
    "# 输出结果\n",
    "print(\"NIR5:1\\n训练集样本编号:\")\n",
    "print(train_samples_ids)\n",
    "print(\"测试集样本编号:\")\n",
    "print(test_samples_ids)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "5c81169c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "ATR2:1\n",
      "训练集样本编号:\n",
      "[14 12  3 18 13 23  9 34 21 30  6 41  2 20 38 10  1 28  8 24 35 19 16 25\n",
      " 22 36 15 32]\n",
      "测试集样本编号:\n",
      "[ 4  5  7 11 17 26 27 29 31 33 37 39 40 42 43]\n"
     ]
    }
   ],
   "source": [
    "# 读取Excel文件，并假设第一列是样本编号，其余列是特征数据\n",
    "df = pd.read_excel(\"F:\\\\研究\\\\大黄不同月份液体近红外KS划分\\\\10.25\\\\ATR.xlsx\", sheet_name='Sheet1')\n",
    "# 第一列是样本编号，其余是特征数据\n",
    "sample_ids = df.iloc[:, 0]  # 保存样本编号\n",
    "x_variables = df.iloc[:, 1:].values  # 转换特征数据为numpy数组\n",
    "\n",
    "# 确保x_variables只包含数值数据\n",
    "x_variables = np.array(x_variables, dtype=float)\n",
    "\n",
    "# 调用k-s算法函数\n",
    "a = kennardstonealgorithm(x_variables, 28)\n",
    "# 得到训练集和测试集索引值\n",
    "train = a[0]\n",
    "test = a[1]\n",
    "# 使用索引值从原始DataFrame中获取样本编号\n",
    "train_samples_ids = sample_ids[train].values\n",
    "test_samples_ids = sample_ids[test].values\n",
    "\n",
    "# 输出结果\n",
    "print(\"ATR2:1\\n训练集样本编号:\")\n",
    "print(train_samples_ids)\n",
    "print(\"测试集样本编号:\")\n",
    "print(test_samples_ids)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "fb080dda",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "ATR3:1\n",
      "训练集样本编号:\n",
      "[14 12  3 18 13 23  9 34 21 30  6 41  2 20 38 10  1 28  8 24 35 19 16 25\n",
      " 22 36 15 32 39 27 42 17]\n",
      "测试集样本编号:\n",
      "[ 4  5  7 11 26 29 31 33 37 40 43]\n"
     ]
    }
   ],
   "source": [
    "# 调用k-s算法函数\n",
    "a = kennardstonealgorithm(x_variables, 32)\n",
    "# 得到训练集和测试集索引值\n",
    "train = a[0]\n",
    "test = a[1]\n",
    "# 使用索引值从原始DataFrame中获取样本编号\n",
    "train_samples_ids = sample_ids[train].values\n",
    "test_samples_ids = sample_ids[test].values\n",
    "\n",
    "# 输出结果\n",
    "print(\"ATR3:1\\n训练集样本编号:\")\n",
    "print(train_samples_ids)\n",
    "print(\"测试集样本编号:\")\n",
    "print(test_samples_ids)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "2e6a6e25",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "ATR4:1\n",
      "训练集样本编号:\n",
      "[14 12  3 18 13 23  9 34 21 30  6 41  2 20 38 10  1 28  8 24 35 19 16 25\n",
      " 22 36 15 32 39 27 42 17  7 33]\n",
      "测试集样本编号:\n",
      "[ 4  5 11 26 29 31 37 40 43]\n"
     ]
    }
   ],
   "source": [
    "# 调用k-s算法函数\n",
    "a = kennardstonealgorithm(x_variables, 34)\n",
    "# 得到训练集和测试集索引值\n",
    "train = a[0]\n",
    "test = a[1]\n",
    "# 使用索引值从原始DataFrame中获取样本编号\n",
    "train_samples_ids = sample_ids[train].values\n",
    "test_samples_ids = sample_ids[test].values\n",
    "\n",
    "# 输出结果\n",
    "print(\"ATR4:1\\n训练集样本编号:\")\n",
    "print(train_samples_ids)\n",
    "print(\"测试集样本编号:\")\n",
    "print(test_samples_ids)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "681c5df2",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "ATR5:1\n",
      "训练集样本编号:\n",
      "[14 12  3 18 13 23  9 34 21 30  6 41  2 20 38 10  1 28  8 24 35 19 16 25\n",
      " 22 36 15 32 39 27 42 17  7 33 40 11]\n",
      "测试集样本编号:\n",
      "[ 4  5 26 29 31 37 43]\n"
     ]
    }
   ],
   "source": [
    "# 调用k-s算法函数\n",
    "a = kennardstonealgorithm(x_variables, 36)\n",
    "# 得到训练集和测试集索引值\n",
    "train = a[0]\n",
    "test = a[1]\n",
    "# 使用索引值从原始DataFrame中获取样本编号\n",
    "train_samples_ids = sample_ids[train].values\n",
    "test_samples_ids = sample_ids[test].values\n",
    "\n",
    "# 输出结果\n",
    "print(\"ATR5:1\\n训练集样本编号:\")\n",
    "print(train_samples_ids)\n",
    "print(\"测试集样本编号:\")\n",
    "print(test_samples_ids)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
